// ==UserScript==
// @name         fzu雨课堂
// @namespace    http://tmpermonkey.net/
// @version      0.1
// @description  fzu雨课堂视频自动播放
// @author       Oliv
// @match        https://*.yuketang.cn/pro/lms/*/*/video/*
// @icon         
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest
// @grant        GM_addStyle
// @grant        GM_getResourceText
// @run-at       document-end
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    function goNext() {
        let url = window.location.href;
        let nextVideoId = parseInt(/video\/([0-9]+)$/.exec(url)[1]) + 1;
        let newUrl = url.replace(/video\/[0-9]+$/, "video/" + nextVideoId);
        window.location.href = newUrl;
    }

    function autoPlayFn() {
        let progress = 'null';

        try {
            progress = document.querySelectorAll('span.text')[1].textContent.split("：")[1];
        } catch (e) {
            let videoBox = document.querySelector('#video-box');
            if (videoBox.innerHTML == '') {
                goNext();
                return;
            } else {
                window.location.reload();
            }
        }

        try {
            progress = parseInt(progress.split("%")[0]);
            document.querySelector('div.title-fl:first-child').innerText = `当前进度：${progress}%`;
            if (progress >= 95) {
                goNext();
                return;
            }
            setTimeout(autoPlayFn, 3000);
        } catch (e) {
            window.location.reload();
        }
    }

    let url = window.location.href;
    const delayTime = 3000;
    if (url.match("/video")) {
        if (window.onurlchange === null) {
            window.addEventListener("urlchange", () => {
                setTimeout(autoPlayFn, delayTime);
            });
        }
        setTimeout(autoPlayFn, delayTime);
    }
})();